<template>
  <el-dialog
    :title="formData.comment"
    :visible="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    width="1000px"
    center
  >
    <el-form
      ref="addEditForm"
      v-loading="isLoading > 0"
      :model="formData"
      label-width="100px"
      class="add-edit-form"
    >
      <el-form-item label="活动">
        <el-button type="primary" @click="addActivityItem()">添加</el-button>
        <ul class="activity-box">
          <li v-for="(item, i) in activityList" :key="i">
            <upload-img v-model="item.img" />
            <el-input v-model="item.to" class="input" placeholder="请输入用户点击该图片去往的页面" />
            <span class="label">显示位置：</span>
            <el-input v-model="item.sort" class="input" style="flex: none;width: 50px;" />
            <i class="icon el-icon-remove" @click="removeActivityItem()" />
          </li>
        </ul>
      </el-form-item>

      <el-form-item class="submit-box">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import lodash from 'lodash'
import UploadImg from '@/components/UploadImg'
import { addOneConfig, updateOneConfig } from '@/api/config'
const emptyFormData = {
  c_value: '',
  status: -1
}
let formData = null
export default {
  name: 'ConfigAddEdit',
  components: {
    UploadImg
  },
  props: {},
  data() {
    return {
      dialogVisible: false,
      isLoading: 0,
      formData: {},
      activityList: []
    }
  },
  computed: {},
  watch: {
    formData: {
      deep: true,
      handler(newVal) {
        console.log(1111111111)
        if (newVal.c_value) {
          this.activityList = JSON.parse(newVal.c_value)
        } else {
          return []
        }
      }
    }
  },
  created() {
  },
  mounted() {},
  methods: {
    removeActivityItem(i) {
      this.activityList.splice(i, 1)
    },
    addActivityItem() {
      this.activityList.push({ img: null, to: null, sort: 1 })
    },
    handleClose() {
      this.dialogVisible = false
    },
    open(row) {
      this.dialogVisible = true
      if (row) {
        formData = lodash.cloneDeep(row)
      } else {
        formData = lodash.cloneDeep(emptyFormData)
      }
      this.resetForm()
    },
    resetForm() {
      this.formData = lodash.cloneDeep(formData)
      this.$nextTick(() => {
        this.$refs.addEditForm.clearValidate()
      })
    },
    submitForm() {
      if (this.isLoading > 0) return
      this.$refs.addEditForm.validate(async vaild => {
        if (!vaild) return
        try {
          this.isLoading++
          this.formData.c_value = JSON.stringify(this.activityList)

          if (this.formData.id) {
            await updateOneConfig(this.formData)
          } else {
            await addOneConfig(this.formData)
          }
          this.$emit('refresh')
          this.handleClose()
        } finally {
          this.isLoading--
        }
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.submit-box {
  display: flex;
  justify-content: flex-end;
}

.activity-box {
  li {
    display: flex;
    align-items: center;
    margin-top: 10px;
  }
  .input {
    margin: 0 10px;
  }
  .label {
    flex: none;
  }
  .icon {
    cursor: pointer;
    font-size: 30px;
    color: #409eff;
  }
}
</style>
